package com.own.business.system.menu.controller.user;

import com.own.business.system.menu.business.service.SystemMenuService;
import com.own.business.system.menu.business.service.SystemRolePermissionService;
import com.own.business.system.menu.common.util.MenuUtil;
import com.own.business.system.menu.entity.map.SystemMenuMapVo;
import com.own.component.client.base.RequestClientManager;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.common.util.list.TreeNode;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * UserSystemMenuController
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Tag(name = "系统菜单" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.USER_CLIENT + "system/menu")
public class UserSystemMenuController extends BaseController {

    @Resource
    private SystemMenuService systemMenuService;
    @Resource
    private SystemRolePermissionService systemRolePermissionService;

    @Resource
    private RequestClientManager requestClientManager;

    @Resource
    private HttpServletRequest request;

    @LogOperationAop
    @Operation(summary = "查询当前用户的菜单树信息")
    @GetMapping("/tree")
    public ResultModel<List<TreeNode<SystemMenuMapVo>>> tree() {
        var loginUser = SessionUserUtil.getLoginUser();
        // 获取客户端信息
        var client = requestClientManager.client(request);
        // 获取当前客户端所有的可用菜单列表
        var menuList = systemMenuService.listByClientId(client, false, null);
        // 获取当前用户的权限值信息
        var permissionMap = systemRolePermissionService.mapMenuPermissionByUserId(loginUser.userId(), client);
        // 进行菜单显示梳理，由于下级出现了可用的权限，上级也要显示，所以需要将所有的可用节点都查询出来，然后依次设置权限值即可
        var menuTreeList = menuList.stream()
                .map(SystemMenuMapVo::new)
                .peek(item -> item.setPermission(MenuUtil.resolvePermission(permissionMap.get(item.getId()))))
                .map(TreeNode::new)
                .toList();
        var treeList = TreeNode.build(menuTreeList, SystemMenuMapVo::getId, SystemMenuMapVo::getParentId);
        // 将没有权限的节点都进行移除
        TreeNode.recurrenceCheck(
                treeList,
                item -> StringUtils.isNotBlank(item.getPermission()),
                item -> item.setChildren(null)
        );
        return R.success(treeList);
    }

    @LogOperationAop
    @Operation(summary = "查询当前用户所有可用的菜单信息")
    @GetMapping("/all")
    public ResultModel<List<SystemMenuMapVo>> all() {
        var loginUser = SessionUserUtil.getLoginUser();
        // 获取客户端信息
        var client = requestClientManager.client(request);
        // 获取当前用户的权限值信息
        var permissionMap = systemRolePermissionService.mapMenuPermissionByUserId(loginUser.userId(), client);
        // 进行菜单显示梳理，由于下级出现了可用的权限，上级也要显示，所以需要将所有的可用节点都查询出来，然后依次设置权限值即可
        var menuList = systemMenuService.listByClientId(client, false, null)
                .stream()
                .map(SystemMenuMapVo::new)
                .peek(item -> item.setPermission(MenuUtil.resolvePermission(permissionMap.get(item.getId()))))
                .toList();
        return R.success(menuList);
    }

}
